package com.gaoyang.util;

import java.io.*;
import java.net.*;
import java.util.Map;

public class HttpUtils {
	public static String postUrl(String requestUrl, Map<String, String> paramMap) {
		StringBuffer strbuffer = new StringBuffer();
		String paramUrl = "";
		if ((paramMap != null) && (paramMap.size() > 0)) {
			for (Map.Entry entry : paramMap.entrySet()) {
				String key = String.valueOf(entry.getKey());
				String value = String.valueOf(entry.getValue());
				strbuffer.append(key);
				strbuffer.append("=");
				strbuffer.append(value);
				strbuffer.append("&");

			}
			paramUrl = strbuffer.toString();
			paramUrl = paramUrl.substring(0, paramUrl.length() - 1);
		}

		HttpURLConnection connection = null;
		StringBuffer sb = new StringBuffer("");
		try {
			URL url = new URL(requestUrl);
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Accept", "*/*");
			connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			connection.setRequestProperty("Cookie", "UM_distinctid=16097096b609d8-043656fc51f8ca-16386656-13c680-16097096b61887; CNZZDATA1256966228=658326972-1514359128-%7C1514359128; PHPEPIAOSESSION=o5i74siab35thd53t04ua64pf5");
			connection.setRequestProperty("Host", "m.epiaom.com");
			connection.setRequestProperty("Origin", "https://m.epiaom.com");
			connection.setRequestProperty("Referer", "https://m.epiaom.com/usercenter/UserMovieCard.html");
			connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
			connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
			connection.connect();
			DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
			writer.writeBytes(paramUrl);
			writer.flush();
			writer.close();

			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
			String lines;
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes());
				sb.append(lines);
			}
			reader.close();
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		return sb.toString();
	}


}
